﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.ComponentModel.Composition;

namespace Microsoft.Internal
{
    internal static class Requires
    {
        [DebuggerStepThrough]
        public static void NotNull(object value, string parameterName)
        {
            if (value == null)
            {
                throw new ArgumentNullException(parameterName);
            }
        }

        [DebuggerStepThrough]
        public static void NotNullOrEmpty(string value, string parameterName)
        {
            NotNull(value, parameterName);

            if (value.Length == 0)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentException_EmptyString, parameterName));
            }
        }

        [DebuggerStepThrough]
        public static void NotNullOrNullElements(IEnumerable values, string parameterName)
        {
            NotNull(values, parameterName);
            NotNullElements(values, parameterName);
        }

        [DebuggerStepThrough]
        public static void NullOrNotNullElements(IEnumerable values, string parameterName)
        {
            if (values != null)
            {
                NotNullElements(values, parameterName);
            }
        }

        private static void NotNullElements(IEnumerable values, string parameterName)
        {
            foreach (object value in values)
            {
                if (value == null)
                {
                    throw ExceptionBuilder.CreateContainsNullElement(parameterName);
                }
            }
        }

        [DebuggerStepThrough]
        public static void IsDefined<TEnum>(TEnum value, string parameterName) where TEnum : struct
        {
            if (!Enum.IsDefined(typeof(TEnum), value))
            {
                // Cannot throw InvalidEnumArgumentException as it is not defined in Silverlight
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentOutOfRange_InvalidEnum, parameterName, value, typeof(TEnum).Name), parameterName);
            }
        }
    }
}
